#include "../inc/Utils.h"

char* get_time()
{
	time_t time_buffer;
	time(&time_buffer);
	return (ctime(&time_buffer));
}

void _print_log(const char* format, ...)
{
	FILE* fd = NULL;
	char* logFile = NULL;
	char* logFileBak = NULL;

		logFile = "/tmp/player.log";
		logFileBak = "/tmp/player.bak.log";
		fd = fopen(logFile,"a");

	va_list ap;
	va_start(ap, format);
	if(fd != NULL)
	{
		fprintf(fd, "%s ", strtok(get_time(), "\n"));
		vfprintf(fd, format, ap);
		fseek (fd, 0, SEEK_END);   
		int size = ftell (fd); 
		va_end(ap);
		fflush(fd);
		fclose(fd);
		if(size > 5 * 1024 * 1024)
		{
			remove(logFileBak);
			rename(logFile, logFileBak);
		}
	}
}
